<?php

if (!defined('TS_ROOT')) die(basename(__FILE__));

$datastore->enqueue(array(
	'ranks',
));

if (empty($_GET[POST_USERS_URL]) || $_GET[POST_USERS_URL] == GUEST_UID)
{
	ts_die($lang['NO_USER_ID_SPECIFIED']);
}
if (!$profiledata = get_userdata($_GET[POST_USERS_URL]))
{
	ts_die($lang['NO_USER_ID_SPECIFIED']);
}

if (!$userdata['session_logged_in'])
{
	redirect(LOGIN_URL . "?redirect={$_SERVER['REQUEST_URI']}");
}

if (!$ranks = $datastore->get('ranks'))
{
	$datastore->update('ranks');
	$ranks = $datastore->get('ranks');
}

$poster_rank = $rank_image= $rank_style = $rank_select = '';
if ($user_rank = $profiledata['user_rank'] AND isset($ranks[$user_rank]))
{
	$rank_image = ($ranks[$user_rank]['rank_image']) ? '<img src="'. $ranks[$user_rank]['rank_image'] .'" alt="" title="" border="0" />' : '';
	$poster_rank = $ranks[$user_rank]['rank_title'];
	$rank_style  = $ranks[$user_rank]['rank_style'];
}
if (IS_ADMIN)
{
	$rank_select = array($lang['NONE'] => 0);
	foreach ($ranks as $row)
	{
		$rank_select[$row['rank_title']] = $row['rank_id'];
	}
	$rank_select = build_select('rank-sel', $rank_select, $user_rank);
}

if (bf($profiledata['user_opt'], 'user_opt', 'user_viewemail') || $profiledata['user_id'] == $userdata['user_id'] || IS_AM)
{
	$email_uri = ($ts_cfg['board_email_form']) ? 'profile.php?mode=email&amp;'. POST_USERS_URL .'='. $profiledata['user_id'] : 'mailto:'. $profiledata['user_email'];
	$email = '<a class="editable" href="'. $email_uri .'">'. $profiledata['user_email'] .'</a>';
}
else
{
	$email = '';
}

$profile_user_id = ($profiledata['user_id'] == $userdata['user_id']);

$template->assign_vars(array(
	'PROFILE_USERS'        => sprintf($lang['VIEWING_USER_PROFILE'], $profiledata['username']),
	'USERNAME'             => $profiledata['username'],
	'PROFILE_USER_ID'      => $profiledata['user_id'],
	'PROFILE_USER'         => $profile_user_id,
	'USER_REGDATE'         => bb_date($profiledata['user_regdate'], 'Y-m-d H:i', false),
	'POSTER_RANK'          => ($poster_rank) ? "<span class=\"$rank_style\">". $poster_rank ."</span>" : $lang['USER'],
	'RANK_IMAGE'           => $rank_image,
	'RANK_SELECT'          => $rank_select,
	
	'EMAIL'                => $email,
	'WWW'                  => $profiledata['user_website'],
	'ICQ'                  => $profiledata['user_icq'],
	'LAST_VISIT_TIME'      => ($profiledata['user_lastvisit']) ? (bf($profiledata['user_opt'], 'user_opt', 'user_viewonline') && !IS_ADMIN) ? $lang['HIDDEN_USER'] : bb_date($profiledata['user_lastvisit'], 'Y-m-d H:i', false) : $lang['NEVER'],
	'LAST_ACTIVITY_TIME'   => ($profiledata['user_session_time']) ? (bf($profiledata['user_opt'], 'user_opt', 'user_viewonline') && !IS_ADMIN) ? $lang['HIDDEN_USER'] : bb_date($profiledata['user_session_time'], 'Y-m-d H:i', false) : $lang['NEVER'],
	'GENDER'               => ($ts_cfg['gender']) ? $lang['GENDER_SELECT'][$profiledata['user_gender']] : '',
	'BIRTHDAY'             => ($ts_cfg['birthday_enabled'] && $profiledata['user_birthday'] != '0000-00-00') ? $profiledata['user_birthday'] : '',
	'AGE'                  => ($ts_cfg['birthday_enabled'] && $profiledata['user_birthday'] != '0000-00-00') ? birthday_age($profiledata['user_birthday']) : '',
	'USER_POST'			   => $profiledata['user_posts'],
	
	'USER_ACTIVE'          => $profiledata['user_active'],
	'LOCATION'             => $profiledata['user_from'],
	'OCCUPATION'           => $profiledata['user_occ'],
	'INTERESTS'            => $profiledata['user_interests'],
	'SKYPE'                => $profiledata['user_skype'],
	'TWITTER'              => $profiledata['user_twitter'],
	
	'FIRSTNAME'            => $profiledata['user_first_name'],
	'SECONDNAME'           => $profiledata['user_second_name'],
	'BOBNAME'              => $profiledata['user_bob_name'],
	'PHONE'            	   => $profiledata['user_phone'],
	'PASPORT'              => $profiledata['user_pasport'],
	'ADRESS'               => $profiledata['user_adress'],
	
	'AVATAR_IMG'           => get_avatar($profiledata['user_id'], $profiledata['avatar_ext_id'], !bf($profiledata['user_opt'], 'user_opt', 'dis_avatar')),

	'SHOW_ROLE'            => (IS_AM || $profile_user_id || $profiledata['user_active']),
	
));

if (IS_ADMIN)
{
	$ajax_user_opt = ts_json_encode(array(
		'dis_avatar'       		   => bf($profiledata['user_opt'], 'user_opt', 'dis_avatar'),
		'dis_post'         		   => bf($profiledata['user_opt'], 'user_opt', 'dis_post'),
		'dis_edit_profile'         => bf($profiledata['user_opt'], 'user_opt', 'dis_edit_profile'),
	));
	
	$template->assign_vars(array(
		'EDITABLE_TPLS'    => true,
		'AJAX_USER_OPT'    => $ajax_user_opt,
		'U_MANAGE'         => "profile.php?mode=editprofile&amp;u={$profiledata['user_id']}",
		'U_PERMISSIONS'    => "admin/admin_ug_auth.php?mode=user&amp;u={$profiledata['user_id']}",
	));
}

$user_restrictions = array();

if (bf($profiledata['user_opt'], 'user_opt', 'dis_avatar'))    		   $user_restrictions[] = $lang['HIDE_AVATARS'];
if (bf($profiledata['user_opt'], 'user_opt', 'dis_post'))      		   $user_restrictions[] = $lang['SEND_MESSAGE'];
if (bf($profiledata['user_opt'], 'user_opt', 'dis_edit_profile'))      $user_restrictions[] = $lang['EDIT_PFOROLES'];

$template->assign_var('USER_RESTRICTIONS', join('<br />', $user_restrictions));

print_page('user_profile.tmpl');